package com.example.httpclient.UI;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bob.library.SpotsDialog;
import com.example.httpclient.Adapter.BottomPopWindows;
import com.example.httpclient.R;
import com.example.httpclient.SystemUtils;
import com.example.httpclient.uilts.printString;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    public static ArrayList<String> UnVisitUrl = new ArrayList<>();
    private Button btn_start, btn_find;
    private TextView tv_link;
    private TextView tv_down_show;
    private SpotsDialog sp;
    private Button btn_open;
    public Handler mHandler = new Handler(Looper.myLooper()) {
        @SuppressLint("SetTextI18n")
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0x00://提示
                    printString.print(MainActivity.this, (String) msg.obj);
                    break;
                case 0x01://数据显示
                    tv_link.setText(String.valueOf(msg.obj) + "\n");
                    break;
                case 0x02://下载时显示的提示
                    tv_down_show.setText(msg.obj+"\n");
                    break;
                case 0x123:
                    tv_link.setText("");
                    ArrayList<String> htmlContents = (ArrayList<String>) msg.obj;
                    if (htmlContents.size() > 0) {
                        for (String ss : htmlContents) {
                            tv_link.append(ss + "\n");
                            if(!UnVisitUrl.contains(ss)){
                                UnVisitUrl.add(ss);
                            }
                        }
                    } else {
                        printString.print(MainActivity.this, "无数据");
                    }
                    break;
                case 0x221:
                    sp.show();
                    break;
                case 0x222:
                    sp.hide();
                    break;
            }
        }
    };


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        boolean isOK=false;
        if(resultCode==RESULT_OK){
            if(requestCode==0000){
                isOK=true;
            }
        }
        if(!isOK){
            SystemUtils.Exit(MainActivity.this);
        }
    }

    /***
     * https://pic.netbian.com/uploads/allimg/190824/212516-1566653116 1ade.jpg
     *https://pic.netbian.com/uploads/allimg/190824/212516-1566653116f355.jpg
     * https://pic.netbian.com/uploads/allimg/210423/224716-1619189236 e4d9.jpg
     * https://pic.netbian.com/uploads/allimg/210423/224716-1619189236 1adb.jpg
     * @param savedInstanceState
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setListener();
        SystemUtils.getPermissions(this);
    }

    private void setListener() {
        myOnclickListener mListener = new myOnclickListener();
        btn_find.setOnClickListener(mListener);
        btn_start.setOnClickListener(mListener);
        btn_open.setOnClickListener(mListener);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void initView() {
        TextView tv_title = findViewById(R.id.tV_title);
        tv_title.setText("Crawler");
        tv_link = findViewById(R.id.txt_link_show);
        tv_down_show=findViewById(R.id.txt_down_show);
        btn_find = findViewById(R.id.btn_find_link);
        btn_start=findViewById(R.id.btn_start);
        sp=new SpotsDialog(MainActivity.this,R.style.Custom);
        btn_open=findViewById(R.id.btn_open);

    }

    private class myOnclickListener implements View.OnClickListener{
        BottomPopWindows bottomPopWindowsDown;
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_find_link:
                    BottomPopWindows bottomPopWindowsFind=new BottomPopWindows(MainActivity.this,0,mHandler);
                    bottomPopWindowsFind.PutDataList();
                    break;
                case R.id.btn_start:
                    bottomPopWindowsDown=new BottomPopWindows(MainActivity.this,1,mHandler);
                    bottomPopWindowsDown.setHrefUrl(UnVisitUrl);
                    bottomPopWindowsDown.PutDataList();
                    break;
                case R.id.btn_open:
                    Intent in=new Intent(MainActivity.this,DataShowActivity.class);
                    startActivity(in);
                    break;
            }
        }
    }
}